package com.cnswhy.cloud.server.app.config;

import com.cnswhy.cloud.common.core.base.boot.BaseConfig;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableConfigurationProperties(WxProperties.class)
//@EnableResourceServer
public class CnswhyServerAppResourceServerConfigure  extends BaseConfig implements WebMvcConfigurer {
    @Bean
    public CnswhyAppInteceptorConfigure cnswhyAppInteceptorConfigure(){
        return new CnswhyAppInteceptorConfigure();
    }

    @Autowired
    WxProperties wxProperties;
    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        com.github.binarywang.wxpay.config.WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(wxProperties.getAppPayId()));
        payConfig.setMchId(StringUtils.trimToNull(wxProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(wxProperties.getMchKey()));
        payConfig.setSignType("MD5");
        payConfig.setNotifyUrl(StringUtils.trimToNull(wxProperties.getNotifyUrl()));
        // payConfig.setKeyPath(StringUtils.trimToNull(wxPayProperties.getKeyPath()));

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
